package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.entity.Evaluate;
import tech.aistar.entity.Register;
import tech.aistar.entity.Result;
import tech.aistar.entity.Tenant;
import tech.aistar.service.*;

@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private IRegisterService registerService;
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private ILandlordService landlordService;
    @Autowired
    private IIntentionService intentionService;
    @Autowired
    private IEvaluateService evaluateService;
    //注册初始页面
    @RequestMapping("/check")
    public String register() {
        return "/login/register";
    }


    //注册成功数据插入表中并且跳登录页面
    @RequestMapping("/adduser/{name}/{password}/{identity}")
    public String addUser(@PathVariable("name") String account, @PathVariable("password") String password, @PathVariable("identity") String identity) {

        Register register = new Register();
        register.setAccount(account);
        register.setPassword(password);

        if (identity.equals("经纪人")) {
            register.setIdentity("2");
            registerService.insert(register);
            Register register1 = registerService.selectByAccount(account);
            agentService.insertById(register1.getId());
            evaluateService.insert(new Evaluate(agentService.selectByregisterId(register1.getId()).getId(),register1.getId()));
        } else if (identity.equals("租客")) {
            register.setIdentity("1");
            registerService.insert(register);
            Register register1 = registerService.selectByAccount(account);
            tenantService.insertById(register1.getId());
            Tenant tenant = tenantService.selectByregisterId(register1.getId());
            intentionService.insertTenantId(tenant.getId());
        } else if (identity.equals("房东")) {
            register.setIdentity("3");
            registerService.insert(register);
            Register register1 = registerService.selectByAccount(account);
            landlordService.insertById(register1.getId());
        }
        return "/login/login";
    }


    //检测用户名账户是否存在，存在不让注册
    @RequestMapping("/checkaccount")
    public @ResponseBody
    Result select(String account) {

        Register register = registerService.selectByAccount(account);
        System.out.println(register.getAccount());
        if (register.getAccount().equals("")) {
            return new Result("200", "修改成功");
        } else {
            return new Result("500", "修改失败");
        }
    }

}